import router from './router'
import { message } from 'ant-design-vue'
import { __getLocal } from '@/utils/tools.js'
import { useAccountStore } from '@/stores/account'
export function beforeEach() {
  router.beforeEach(async (to, from, next) => {
    const hasToken = __getLocal('AUTH_TOKEN')
    // console.log(' hasToken', hasToken)
    if (hasToken) {
      next()
    } else {
      const accountStore = useAccountStore()
      try {
        // get user token
        await accountStore.getToken()
        next()
      } catch (error) {
        // remove token and go to login page to re-login
        accountStore.resetToken()
        message.error(error.message || '未知错误')
        next()
      }
    }
  })
}
